<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="java.util.Date" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>个人中心 - 校园二手市场</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Noto Serif SC", "Source Han Serif SC", "SimSun",
            "Times New Roman", serif;
        }

        body {
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        a {
            text-decoration: none;
            color: inherit;
        }

        .container {
            width: 90%;
            max-width: 1000px;
            margin: 0 auto;
        }

        .header {
            background-color: #2c3e50;
            color: white;
            padding: 30px 0;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            max-width: 100%;
        }

        .header .container {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo h1 {
            font-size: 3rem;
            display: flex;
            align-items: center;
            font-weight: 700;
            min-width: 400px;
            letter-spacing: 2px;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
        }

        .logo i {
            margin-right: 15px;
            font-size: 1.8rem;
        }

        .logo i.fa-recycle {
            font-size: 3rem;
            margin-right: 15px;
            vertical-align: middle;
            color: #e74c3c;
        }

        .user-menu ul {
            display: flex;
            list-style: none;
            gap: 15px;
        }

        .user-menu li {
            margin-left: 25px;
            min-width: 100px;
        }

        .user-menu a {
            display: flex;
            align-items: center;
            font-size: 0.9rem;
            font-weight: 500;
            transition: color 0.2s;
            padding: 4px 0;
        }

        .user-menu a:hover {
            color: #e74c3c;
        }

        .user-menu i {
            margin-right: 5px;
            font-size: 1rem;
        }


        .profile-container {
            background-color: white;
            border-radius: 6px;
            box-shadow: 0 2px 15px rgba(0, 0, 0, 0.08);
            margin: 25px auto;
            overflow: hidden;
            width: 100%;
            max-width: 1000px;
            height: 100%;
            flex: 1;
        }

        .profile-header {
            background-color: #2c3e50;
            color: white;
            padding: 30px;
            display: flex;
            align-items: center;
            position: relative;
        }

        .profile-info {
            flex: 1;
        }

        .profile-info h2 {
            font-size: 2.5rem;
            margin-bottom: 6px;
        }

        /* 头像容器样式 */
        .avatar-container {
            position: relative;
            width: 120px;
            height: 120px;
            border-radius: 50%;
            overflow: hidden;
            border: 3px solid white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
            background-color: #f0f0f0;
            margin-right: 20px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .avatar-container:hover {
            transform: scale(1.05);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        }

        .avatar-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .avatar-container.img-error .avatar-text {
            display: block;
        }

        .avatar-container.img-error img {
            display: none;
        }

        .avatar-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #7f8c8d;
            font-size: 14px;
            font-weight: 500;
            display: none;
            z-index: 1;
        }

        /* 更换头像按钮样式 */
        .change-avatar-btn {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: rgba(0, 0, 0, 0.6);
            color: white;
            border: none;
            padding: 8px;
            font-size: 0.9rem;
            text-align: center;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s;
        }

        .change-avatar-btn i {
            margin-right: 5px;
        }

        .change-avatar-btn:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }

        .stats-container {
            display: flex;
            gap: 20px;
            margin: 20px 0;
            flex-wrap: wrap;
            justify-content: space-between;
        }

        /* 单个统计卡片 */
        .stat-card {
            flex: 1;
            min-width: 120px;
            background: #fff;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }

        /* 统计图标 */
        .stat-icon {
            font-size: 24px;
            color: #e74c3c;
            margin-bottom: 10px;
        }

        /* 统计数值 */
        .stat-value {
            font-size: 30px;
            font-weight: bolder;
            color: #2c3e50;
            margin: 5px 0;
        }

        /* 统计标签 */
        .stat-label {
            font-size: 14px;
            color: #7f8c8d;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .stat-card {
                min-width: calc(50% - 10px);
            }
        }

        @media (max-width: 480px) {
            .stat-card {
                min-width: 100%;
            }
        }


        .avatar-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }

        .avatar-modal-content {
            background-color: white;
            padding: 25px;
            border-radius: 8px;
            width: 90%;
            max-width: 500px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .avatar-preview-container {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 0 auto 20px;
            border-radius: 50%;
            overflow: hidden;
            border: 3px solid #eee;
            background-color: #f0f0f0;
        }

        .avatar-preview-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #7f8c8d;
            font-size: 16px;
            font-weight: 500;
            display: none;
            z-index: 1;
        }

        .avatar-preview-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            position: relative;
            z-index: 2;
        }

        .avatar-preview-container.img-error .avatar-preview-text {
            display: block;
        }

        .avatar-preview-container.img-error img {
            display: none;
        }

        .avatar-actions {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        .avatar-actions button {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s;
        }

        .avatar-actions .save-btn {
            background-color: #2ecc71;
            color: white;
        }

        .avatar-actions .save-btn:hover {
            background-color: #27ae60;
        }

        .avatar-actions .cancel-btn {
            background-color: #e74c3c;
            color: white;
        }

        .avatar-actions .cancel-btn:hover {
            background-color: #c0392b;
        }


        .edit-profile-btn {
            position: absolute;
            right: 25px;
            top: 70px;
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1rem;
            transition: background-color 0.2s;
            display: flex;
            align-items: center;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
        }

        .edit-profile-btn i {
            margin-right: 5px;
        }

        .edit-profile-btn:hover {
            background-color: #2980b9;
        }

        .profile-content {
            padding: 25px;
        }

        .profile-meta {
            margin: 15px 0;
            display: flex;
            flex-wrap: wrap;
            gap: 25px;
        }

        .meta-item {
            font-size: 0.95rem;
            color: #ecf0f1;
            display: flex;
            align-items: center;
        }

        .meta-item i {
            margin-right: 5px;
            font-size: 0.8rem;
        }

        .action-buttons {
            background-color: #f8f9fa;
            padding: 15px 0;
            border-top: 1px solid #eee;
            border-bottom: 1px solid #eee;
        }

        .action-buttons .container {
            display: flex;
            justify-content: center;
            gap: 30px;
        }

        .action-btn {
            padding: 10px 25px;
            border-radius: 4px;
            font-weight: 500;
            transition: all 0.2s;
            display: flex;
            align-items: center;
        }

        .action-btn i {
            margin-right: 8px;
        }

        .home-btn {
            background-color: #3498db;
            color: white;
        }

        .home-btn:hover {
            background-color: #2980b9;
        }

        .logout-btn {
            background-color: #e74c3c;
            color: white;
        }

        .logout-btn:hover {
            background-color: #c0392b;
        }

        @media (max-width: 576px) {
            .profile-meta {
                flex-direction: column;
                gap: 5px;
            }

            .action-buttons .container {
                flex-direction: column;
                gap: 10px;
            }

            .action-btn {
                justify-content: center;
            }
        }

        .section-title {
            font-size: 1.3rem;
            color: #2c3e50;
            margin-bottom: 18px;
            padding: 10px;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
        }

        .section-title i {
            margin-right: 8px;
            color: #e74c3c;
            font-size: 1.1rem;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 16px;
            padding: 15px;
            margin-bottom: 20px;
        }

        .info-card {
            background-color: white;
            border-radius: 4px;
            padding: 16px;
            transition: all 0.2s;
            border: 1px solid #eee;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        }

        .info-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
            border-color: #ddd;
        }

        .info-label {
            color: #7f8c8d;
            font-size: 0.82rem;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .info-value {
            color: #34495e;
            font-size: 1rem;
            font-weight: 600;
        }

        .info-value.null-value {
            color: #95a5a6;
            font-style: italic;
            font-weight: normal;
        }

        .error-message {
            color: #e74c3c;
            text-align: center;
            margin: 30px 0;
            font-size: 1.1rem;
            padding: 15px;
            background-color: rgba(231, 76, 60, 0.1);
            border-radius: 8px;
            border-left: 5px solid #e74c3c;
        }

        .success-message {
            color: #27ae60;
            text-align: center;
            margin: 30px 0;
            font-size: 1.1rem;
            padding: 15px;
            background-color: rgba(39, 174, 96, 0.1);
            border-radius: 8px;
            border-left: 5px solid #27ae60;
        }


        .status-active {
            color: #27ae60;
        }

        .status-inactive {
            color: #e74c3c;
        }

        .footer {
            text-align: center;
            background-color: #2c3e50;
            color: #ecf0f1;
            padding: 18px 9px;
            font-size: 1rem;
            margin-top: auto;
        }

        @media (max-width: 768px) {
            .header .container {
                flex-direction: column;
                gap: 12px;
            }

            .user-menu ul {
                flex-wrap: wrap;
                justify-content: center;
                gap: 12px;
            }

            .profile-header {
                flex-direction: column;
                text-align: center;
                padding: 20px 15px 60px;
            }

            .avatar-container {
                margin-right: 0;
                margin-bottom: 15px;
            }

            .edit-profile-btn {
                position: static;
                margin: 15px auto 0;
                width: fit-content;
            }

            .stats-container {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 576px) {
            .logo h1 {
                font-size: 1.4rem;
            }

            .profile-header {
                padding: 18px 12px 50px;
            }

            .avatar-container {
                width: 70px;
                height: 70px;
            }

            .profile-info h2 {
                font-size: 1.3rem;
            }

            .info-grid {
                grid-template-columns: 1fr;
            }

            .profile-content {
                padding: 18px 12px;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<header class="header">
    <div class="container">
        <div class="logo">
            <a href="${pageContext.request.contextPath}/home"><h1><i class="fas fa-recycle"></i> 校园二手市场</h1></a>
        </div>
        <nav class="user-menu">
            <ul>
                <li>
                    <a href="${pageContext.request.contextPath}/showAllOrder">
                        <i class="fas fa-shopping-cart"></i> 我的订单
                    </a>
                </li>
                <li>
                    <a href="${pageContext.request.contextPath}/productUpload">
                        <i class="fas fa-plus-circle"></i> 发布商品
                    </a>
                </li>
                <li><a href="${pageContext.request.contextPath}/productCenter"><i class="fas fa-box-open"></i> 我的商品</a>
                </li>
            </ul>
        </nav>
    </div>
</header>

<!-- 个人中心内容 -->
<main class="profile-container">
    <!-- 用户头部信息 -->
    <div class="profile-header">
        <div class="avatar-container" onclick="openAvatarModal()">
            <c:choose>
                <c:when test="${not empty user.avatar}">
                    <img src="${pageContext.request.contextPath}/userPictures/${user.userId}/${user.avatar}"
                         onload="hideAvatarText(this)"
                         onerror="showAvatarText(this)">
                </c:when>
                <c:otherwise>
                    <img src="${pageContext.request.contextPath}/default-avatar.jpg" onerror="showAvatarText(this)">
                </c:otherwise>
            </c:choose>
            <div class="avatar-text">用户头像</div>
            <button class="change-avatar-btn">
                <i class="fas fa-camera"></i> 更换头像
            </button>
        </div>
        <div class="profile-info">
            <h2>${user.username}</h2>
            <div class="profile-meta">
                <span class="meta-item"><i
                        class="fas fa-id-card"></i> ${user.studentId != null ? user.studentId : '未设置学号'}</span>
                <span class="meta-item"><i
                        class="fas fa-envelope"></i> ${user.email != null ? user.email : '未设置邮箱'}</span>
                <span class="meta-item"><i class="fas fa-phone"></i> ${user.phone != null ? user.phone : '未设置手机'}</span>
            </div>
        </div>
        <button class="edit-profile-btn" onclick="location.href='${pageContext.request.contextPath}/update'">
            <i class="fas fa-edit"></i> 编辑资料
        </button>
    </div>

    <c:if test="${not empty errorMessage}">
        <div class="error-message">${errorMessage}</div>
    </c:if>
    <c:if test="${not empty successMessage}">
        <div class="success-message">${successMessage}</div>
    </c:if>
    <c:if test="${not empty successUpload}">
        <div class="success-message">${successUpload}</div>
    </c:if>

    <!-- 用户详细信息 -->
    <div class="profile-content">
        <div class="stats-container">
            <div class="stat-card"
                 onclick="window.location.href='${pageContext.request.contextPath}/productCenter?tab=onsale'">
                <div class="stat-icon"><i class="fas fa-box-open"></i></div>
                <div class="stat-value">${productOnsaleQuantity != null ? productOnsaleQuantity : 0}</div>
                <div class="stat-label">在售商品</div>
            </div>
            <div class="stat-card"
                 onclick="window.location.href='${pageContext.request.contextPath}/productCenter?tab=sold'">
                <div class="stat-icon"><i class="fas fa-handshake"></i></div>
                <div class="stat-value">${productSoldQuantity != null ? productSoldQuantity : 0}</div>
                <div class="stat-label">已售商品</div>
            </div>
            <div class="stat-card"
                 onclick="window.location.href='${pageContext.request.contextPath}/productCenter?tab=purchased'">
                <div class="stat-icon"><i class="fas fa-shopping-bag"></i></div>
                <div class="stat-value">${productPurchasedQuantity != null ? productPurchasedQuantity : 0}</div>
                <div class="stat-label">已购商品</div>
            </div>
        </div>
    </div>

    <!-- 基本信息部分 -->
    <h3 class="section-title"><i class="fas fa-id-badge"></i> 基本信息</h3>
    <div class="info-grid">
        <div class="info-card">
            <div class="info-label">用户名</div>
            <div class="info-value">${user.username}</div>
        </div>
        <div class="info-card">
            <div class="info-label">真实姓名</div>
            <div class="info-value ${user.realName == null ? 'null-value' : ''}">
                ${user.realName != null ? user.realName : '未设置'}
            </div>
        </div>
        <div class="info-card">
            <div class="info-label">学号</div>
            <div class="info-value ${user.studentId == null ? 'null-value' : ''}">
                ${user.studentId != null ? user.studentId : '未设置'}
            </div>
        </div>
        <div class="info-card">
            <div class="info-label">账号状态</div>
            <div class="info-value">
                <c:choose>
                    <c:when test="${user.status == 1}">
                        <span class="status-active"><i class="fas fa-check-circle"></i> 正常</span>
                    </c:when>
                    <c:otherwise>
                        <span class="status-inactive"><i class="fas fa-times-circle"></i> 禁用</span>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>
        <div class="info-card">
            <div class="info-label">注册时间</div>
            <div class="info-value">
                <fmt:formatDate value="${user.registerTime}" pattern="yyyy-MM-dd HH:mm"/>
            </div>
        </div>
        <div class="info-card">
            <div class="info-label">上次登录时间</div>
            <div class="info-value">
                <c:choose>
                    <c:when test="${not empty user.lastLoginTime}">
                        <fmt:formatDate value="${user.lastLoginTime}" pattern="yyyy-MM-dd HH:mm"/>
                    </c:when>
                    <c:otherwise>
                        初次登录
                    </c:otherwise>
                </c:choose>
            </div>
        </div>
    </div>

    <!-- 联系信息部分 -->
    <h3 class="section-title"><i class="fas fa-address-book"></i> 联系信息</h3>
    <div class="info-grid">
        <div class="info-card">
            <div class="info-label"><i class="fas fa-envelope"></i> 电子邮箱</div>
            <div class="info-value ${user.email == null ? 'null-value' : ''}">
                ${user.email != null ? user.email : '未设置'}
            </div>
        </div>
        <div class="info-card">
            <div class="info-label"><i class="fas fa-phone"></i> 手机号码</div>
            <div class="info-value ${user.phone == null ? 'null-value' : ''}">
                ${user.phone != null ? user.phone : '未设置'}
            </div>
        </div>
        <div class="info-card">
            <div class="info-label"><i class="fas fa-map-marker-alt"></i> 收货地址</div>
            <div class="info-value ${user.address == null || user.address.isEmpty() ? 'null-value' : ''}">
                ${user.address != null && !user.address.isEmpty() ? user.address : '未设置'}
            </div>
        </div>
    </div>
    </div>
    <div class="action-buttons">
        <div class="container">
            <a href="${pageContext.request.contextPath}/" class="action-btn home-btn">
                <i class="fas fa-home"></i> 返回主页
            </a>
            <a href="${pageContext.request.contextPath}/logout" class="action-btn logout-btn">
                <i class="fas fa-sign-out-alt"></i> 注销用户
            </a>
        </div>
    </div>
</main>

<!-- 头像上传表单 -->
<div id="avatarModal" class="avatar-modal">
    <div class="avatar-modal-content">
        <h3 style="text-align: center; margin-bottom: 20px;"><i class="fas fa-camera"></i> 更换头像</h3>
        <div class="avatar-preview-container">
            <img id="avatarPreview" src=""
                 alt="图像预览"
                 onload="hidePreviewText(this)"
                 onerror="showPreviewText(this)">
            <div class="avatar-preview-text">图像预览</div>
            <div class="avatar-preview-text">图像预览</div>
        </div>
        <form id="avatarForm" action="${pageContext.request.contextPath}/uploadAvatar" method="post"
              enctype="multipart/form-data">
            <div style="text-align: center; margin-bottom: 20px;">
                <input type="file" id="avatarInput" name="avatar" accept="image/*" style="display: none;"
                       onchange="previewImage(this)">
                <button type="button" class="btn" onclick="document.getElementById('avatarInput').click()"
                        style="padding: 8px 15px; background: #3498db; color: white; border: none; border-radius: 4px; cursor: pointer;">
                    <i class="fas fa-upload"></i> 选择图片
                </button>
            </div>
            <div class="avatar-actions">
                <button type="button" class="cancel-btn" onclick="closeAvatarModal()">
                    <i class="fas fa-times"></i> 取消
                </button>
                <button type="submit" class="save-btn">
                    <i class="fas fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
</div>

<!-- 页脚 -->
<footer class="footer">
    <div class="container">
        <p>&copy; 2025 CB 校园二手市场 版权所有</p>
    </div>
</footer>

<script>
    // 定义 showAvatarText 函数，接收图片元素参数
    function showAvatarText(img) {
        const container = img.closest('.avatar-container');
        if (container) {
            container.classList.add('img-error');
        }
    }

    // 定义 hideAvatarText 函数，接收图片元素参数
    function hideAvatarText(img) {
        const container = img.closest('.avatar-container');
        if (container) {
            container.classList.remove('img-error');
        }
    }

    // 定义 showPreviewText 函数，接收图片元素参数
    function showPreviewText(img) {
        const container = img.closest('.avatar-preview-container');
        if (container) {
            container.classList.add('img-error');
        }
    }

    // 定义 hidePreviewText 函数，接收图片元素参数
    function hidePreviewText(img) {
        const container = img.closest('.avatar-preview-container');
        if (container) {
            container.classList.remove('img-error');
        }
    }

    // 打开头像上传模态框
    function openAvatarModal() {
        document.getElementById('avatarModal').style.display = 'flex';
    }

    // 关闭头像上传模态框
    function closeAvatarModal() {
        document.getElementById('avatarModal').style.display = 'none';
    }

    document.getElementById('avatarInput').addEventListener('change', function () {
        const file = this.files[0];
        if (file) {
            // 检查文件类型
            if (!file.type.startsWith('image/')) {
                alert('请选择图片文件');
                return;
            }

            // 检查文件大小 (限制2MB)
            if (file.size > 10 * 1024 * 1024) {
                alert('图片大小不能超过2MB');
                return;
            }

            // 预览图片
            const reader = new FileReader();
            reader.onload = function (e) {
                document.getElementById('avatarPreview').src = e.target.result;
            };
            reader.readAsDataURL(file);

            // 确保模态框是打开的
            document.getElementById('avatarModal').style.display = 'flex';
        }
    });

    // 点击头像容器打开上传模态框
    document.querySelector('.avatar-container').addEventListener('click', function (e) {
        // 确保点击的不是子元素
        if (e.target === this || e.target.tagName === 'IMG') {
            openAvatarModal();
        }
    });
</script>
</body>
</html>